import md5 from './md5.js';

const createRandom = () => {
  const random = Math.random() * 10000;
  return md5.hexMD5(random);
}
const createSign = (params, api_key) => {
  const list = new Array();
  for (let key in params) {
    list.push({ key: key, value: params[key] });
  }
  list.sort((a, b) => {
    if (a.key > b.key) {
      return 1;
    } else if (a.key == b.key) {
      return 0;
    } else {
      return -1;
    }
  });
  let v = "";
  list.map((item, index, array) => {
    if (item.value && item.value != '' && item.key && item.key != 'sign' && item.key != 'key') {
      v += item.key + "=" + item.value + "&";
    }
  })
  v += "key=" + api_key;
  console.log(v);
  return md5.hexMD5(v).toUpperCase();
}
module.exports = {
  createRandom: createRandom,
  createSign: createSign
}